package org.batatasframework.support.database.sql;

import org.batatasframework.support.database.TableInfo;
import org.batatasframework.support.database.metadatas.FieldMetadata;
import org.batatasframework.support.database.metadatas.TableMetadata;
import org.batatasframework.support.database.sql.field.ArraySelectFragment;
import org.batatasframework.support.database.sql.field.ConcatSelectFragment;
import org.batatasframework.support.database.sql.field.DefaultSelectFragment;
import org.batatasframework.support.database.sql.field.FieldElement;
import org.batatasframework.support.database.sql.field.FormatSelectFragment;
import org.batatasframework.support.database.sql.sort.SortFragment;
import org.batatasframework.support.database.sql.sort.SortType;
import org.batatasframework.support.database.sql.spi.TableCriterion;
import org.batatasframework.support.database.sql.where.QueryFragment;
import org.batatasframework.support.definition.QueryDefinition;
import org.batatasframework.support.definition.TableDefinitionResource;
import org.batatasframework.support.event.HttpRequestAccessor;

public class TableCriterionProxy<T extends TableCriterion> implements TableCriterion{
	protected final T sourceTableCriterion;
	
	public TableCriterionProxy(T proxy){
		this.sourceTableCriterion = proxy;
	}

	public String getAlias() {
		return sourceTableCriterion.getAlias();
	}

	public TableRuntimeJoin join(String name) throws Exception {
		return sourceTableCriterion.join(name);
	}

	public SortFragment orderBy(SortFragment fieldSortable) {
		return sourceTableCriterion.orderBy(fieldSortable);
	}

	public TableRuntimeJoin join(TableInfo tableInfo) throws Exception {
		return sourceTableCriterion.join(tableInfo);
	}

	public SortFragment orderBy(TableInstance tableInstance,
			String... fieldNames) {
		return sourceTableCriterion.orderBy(tableInstance, fieldNames);
	}

	public SortFragment orderBy(TableInstance tableInstance, String fieldName) {
		return sourceTableCriterion.orderBy(tableInstance, fieldName);
	}

	public SortFragment orderBy(TableInstance tableInstance,
			FieldMetadata fieldMetadata, SortType sortType, int ordinal) {
		return sourceTableCriterion.orderBy(tableInstance, fieldMetadata,
				sortType, ordinal);
	}

	public SortFragment orderBy(FieldElement fieldElement, SortType sortType,
			int ordinal) {
		return sourceTableCriterion.orderBy(fieldElement, sortType, ordinal);
	}

	public DefaultSelectFragment[] field(TableInstance tableInstance,
			String... names) throws Exception {
		return sourceTableCriterion.field(tableInstance, names);
	}

	public QueryFragment addQuery(QueryFragment queryFragment) throws Exception {
		return sourceTableCriterion.addQuery(queryFragment);
	}

	public ConcatSelectFragment concat(String title) throws Exception {
		return sourceTableCriterion.concat(title);
	}

//	public QueryFragment newQuery(QueryDefinition queryDefine, Opt opt,
//			String[] values) throws Exception {
//		return sourceTableCriterion.newQuery(queryDefine, opt, values);
//	}
	public void newQuery(QueryDefinition queryDefine, HttpRequestAccessor paramAccessor, String key) throws Exception{
		sourceTableCriterion.newQuery(queryDefine, paramAccessor, key);
	}
	public TableMetadata getTableMetadata() {
		return sourceTableCriterion.getTableMetadata();
	}

	public TableRuntimeJoin getJoin(TableMetadata tableMetadata)
			throws Exception {
		return sourceTableCriterion.getJoin(tableMetadata);
	}

	public QueryFragment simpleQuery(TableInstance tableInstance,
			String simpleQuery) {
		return sourceTableCriterion.simpleQuery(tableInstance, simpleQuery);
	}

	public TableRuntimeJoin newJoin(TableDefinitionResource tableDefinition, TableMetadata tableMetadata)
			throws Exception {
		return sourceTableCriterion.newJoin(tableDefinition, tableMetadata);
	}

	public DefaultSelectFragment field(TableInstance tableSource,
			FieldMetadata fieldMetadata) {
		return sourceTableCriterion.field(tableSource, fieldMetadata);
	}

	public TableInstance safeLoadSource(TableCriterion tableCriterion,
			TableDefinitionResource tableDefinition, boolean createIfNeed)
			throws Exception {
		return sourceTableCriterion.safeLoadSource(tableCriterion,
				tableDefinition, createIfNeed);
	}

	public ArraySelectFragment array(String title) throws Exception {
		return sourceTableCriterion.array(title);
	}

	public FormatSelectFragment format(String title, String formatString)
			throws Exception {
		return sourceTableCriterion.format(title, formatString);
	}

	public FieldElement loadFieldElement(TableInstance tableSource,
			FieldMetadata fieldMetadata) {
		return sourceTableCriterion
				.loadFieldElement(tableSource, fieldMetadata);
	}

	public TableRuntimeJoin join(TableInstance table, TableInfo tableInfo)
			throws Exception {
		return sourceTableCriterion.join(table, tableInfo);
	}

	public TableRuntimeJoin join(TableInstance table, String name)
			throws Exception {
		return sourceTableCriterion.join(table, name);
	}
	
}
